//
//  BYNewFeatureViewController.m
//  School+
//
//  Created by Sean Sebastian on 16/5/5.
//  Copyright © 2016年 Sean Sebastian. All rights reserved.
//

#import "BYNewFeatureViewController.h"

#import "Masonry.h"
#import "AppDelegate.h"

#define reuseIdentifier @"Cell"

// 自定义cell
@interface NewFeatureViewCell : UICollectionViewCell

@property (nonatomic , assign) int imageIndex;
@property (nonatomic , weak) UIImageView *backgroundImageView;
@property (nonatomic , weak) UIButton *startButton;

@end

@implementation NewFeatureViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame]) {
        [self prepareUI];
    }
    return self;
}

- (void)prepareUI
{
    [self.contentView addSubview:self.backgroundImageView];
    [self.contentView addSubview:self.startButton];
    
    [self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.contentView);
        make.size.equalTo(self.contentView);
    }];
    
    [self.startButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).with.offset(-5);
        make.bottom.equalTo(self.contentView).with.offset(-100);
        
        make.width.mas_equalTo(@150);
        make.height.mas_equalTo(@30);
        
    }];
}

- (void)startButtonAnimation
{
    self.startButton.hidden = NO;
    // 把按钮的 transform 缩放设置为0
    self.startButton.transform = CGAffineTransformMakeScale(0, 0);
    
    [UIView animateWithDuration:1 delay:0.1 usingSpringWithDamping:0.5 initialSpringVelocity:5 options:1 animations:^{
        
        self.startButton.transform = CGAffineTransformIdentity;
        
    } completion:nil];
    
}

- (UIImageView *)backgroundImageView
{
    if (_backgroundImageView == nil) {
        UIImageView *imageView = [[UIImageView alloc] init];
        
        _backgroundImageView = imageView;
        return imageView;
    }
    return _backgroundImageView;
}

- (UIButton *)startButton
{
    if (_startButton == nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.hidden = YES;
        
        [btn setTitle:@"  getting start！" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(startButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [btn setBackgroundColor:[UIColor blackColor]];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
        btn.titleLabel.font = [UIFont systemFontOfSize:18];
        
        
        _startButton = btn;
        return btn;
    }
    return _startButton;
}

- (void)startButtonClick
{
    NSLog(@"点击开始按钮！！！");
    
    // 这里要强制转换一下，不然会报错！
    [(AppDelegate *)[UIApplication sharedApplication].delegate switchRootController:YES];

}

@end

////////////////////////////////////////////////////////////////////////////////////

@interface BYNewFeatureViewController () <UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic , strong) NSMutableArray *images;

@property (nonatomic , weak) UICollectionView *collectionView;
@property (nonatomic , weak) UICollectionViewFlowLayout *layout;

@property (nonatomic , assign) int itemCount;


@end

@implementation BYNewFeatureViewController

- (NSArray *)images
{
    if (_images == nil) {
        _images = [[NSMutableArray alloc] init];
        for (int i = 0 ; i < self.itemCount ; i++) {
            [_images addObject:[NSString stringWithFormat:@"new_feature_%d" , i+1]];
        }
    }
    return _images;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = [UIScreen mainScreen].bounds.size;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.layout = layout;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.pagingEnabled = YES;
    collectionView.bounces = NO;
    
    self.collectionView = collectionView;
    
    [self.view addSubview:collectionView];
    
    [collectionView registerClass:[NewFeatureViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.itemCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NewFeatureViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.imageIndex = (int)indexPath.item;
    
    cell.backgroundImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"new_feature_%d" , cell.imageIndex + 1]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 正在显示的cell的indexPath
    NSIndexPath *showIndexPath = [collectionView.indexPathsForVisibleItems firstObject];
    NewFeatureViewCell *nowCell = (NewFeatureViewCell *)[collectionView cellForItemAtIndexPath:showIndexPath];
    
    if (showIndexPath.item == self.itemCount - 1) {
        //开始按钮动画
        [nowCell startButtonAnimation];
    }
    
}

- (int)itemCount
{
    return 3;
}
@end

